package com.wpml.cloud.myprovider.interceptor;

import cn.hutool.core.util.RandomUtil;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @Description 日志输出时添加 logId 便于日志跟踪
 * <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss:SSS} [ LOGID:%X{logId} ] [%t] [%p] - %l - %m%n" />
 * @Date 2021/5/13 10:08
 * @Author wangpei
 **/
@Configuration
public class LogInterceptorWebFlux implements WebFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        String logId = RandomUtil.randomString(RandomUtil.BASE_CHAR_NUMBER, 18);
        ThreadContext.put("logId", logId);
        return chain.filter(exchange).then(Mono.empty());
    }
/*@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String logId = RandomUtil.randomString(RandomUtil.BASE_CHAR_NUMBER, 18);
        ThreadContext.put("logId", logId);
        return true;
    }*/
}
